import { defineStore } from 'pinia'
import { useMenuNavApi } from '@/api/menu'
import { generateRoutes, createRouteList, createMenuList } from '@/router'
import { RouteRecordRaw } from 'vue-router'

export const routerStore = defineStore('routerStore', {
	state: () => ({
		menuRoutes: [] as RouteRecordRaw[],
        allRoutes: [],
		searchMenu: [] as RouteRecordRaw[],
		routes: [] as RouteRecordRaw[]
	}),
	actions: {
		async getMenuRoutes() {
			const { data } = await useMenuNavApi()
            const menuList = createMenuList(data);
            if (import.meta.env.DEV) {
                menuList.push({
                    "id": 100000,
                    "name": "备用菜单",
                    "url": null,
                    "openStyle": 0,
                    "icon": "icon-barchart",
                    "children": [
                        // {
                        //   "id": 10000030,
                        //   "name": "油性",
                        //   "url": "admin/statistics/yx",
                        //   "openStyle": 0,
                        //   "icon": "icon-unorderedlist"
                        // },
                      {
                        "id": 10000031,
                        "name": "堆叠面积图",
                        "url": "zother/charts/AreaStack",
                        "openStyle": 0,
                        "icon": "icon-unorderedlist"
                      },
                      {
                        "id": 10000032,
                        "name": "虚线柱状图",
                        "url": "zother/charts/CategoryStack",
                        "openStyle": 0,
                        "icon": "icon-unorderedlist"
                      },
                      {
                        "id": 10000033,
                        "name": "上证指数图",
                        "url": "zother/charts/Candlestick",
                        "openStyle": 0,
                        "icon": "icon-unorderedlist"
                      },
                      {
                        "id": 10000041,
                        "name": "富文本编辑器",
                        "url": "zother/editor/WangEditor",
                        "openStyle": 0,
                        "icon": "icon-unorderedlist"
                      },
                      {
                        "id": 10000042,
                        "name": "Markdown编辑器",
                        "url": "zother/editor/MdEditor",
                        "openStyle": 0,
                        "icon": "icon-unorderedlist"
                      },
                      {
                        "id": 10000051,
                        "name": "Icon 图标",
                        "url": "zother/icons/index",
                        "openStyle": 0,
                        "icon": "icon-unorderedlist"
                      },
                      {
                        "id": 10000052,
                        "name": "二维码生成",
                        "url": "zother/qrcode/index",
                        "openStyle": 0,
                        "icon": "icon-unorderedlist"
                      },
                      {
                        "id": 10000053,
                        "name": "页面打印",
                        "url": "zother/printJs/index",
                        "openStyle": 0,
                        "icon": "icon-unorderedlist"
                      },
                      {
                        "id": 10000054,
                        "name": "图片裁剪",
                        "url": "zother/cropper/index",
                        "openStyle": 0,
                        "icon": "icon-unorderedlist"
                      },
                      {
                        "id": 10000055,
                        "name": "复制文本",
                        "url": "zother/copy/index",
                        "openStyle": 0,
                        "icon": "icon-unorderedlist"
                      },
                      {
                        "id": 100000101,
                        "name": "菜单1",
                        "url": null,
                        "openStyle": 0,
                        "icon": "icon-unorderedlist",
                        "children": [
                          {
                            "id": 1000001011,
                            "name": "菜单11",
                            "url": "system/menu/menu1/menu11/index",
                            "openStyle": 0,
                            "icon": "icon-unorderedlist"
                          },
                          {
                            "id": 1000001012,
                            "name": "菜单13",
                            "url": null,
                            "openStyle": 0,
                            "icon": "icon-unorderedlist",
                            "children": [
                              {
                                "id": 10000010121,
                                "name": "菜单121",
                                "url": "system/menu/menu1/menu12/menu121/index",
                                "openStyle": 0,
                                "icon": "icon-unorderedlist"
                              },
                              {
                                "id": 10000010122,
                                "name": "菜单122",
                                "url": "system/menu/menu1/menu12/menu122/index",
                                "openStyle": 0,
                                "icon": "icon-unorderedlist"
                              }
                            ]
                          },
                          {
                            "id": 1000001013,
                            "name": "菜单13",
                            "url": "system/menu/menu1/menu13/index",
                            "openStyle": 0,
                            "icon": "icon-unorderedlist"
                          }
                        ]
                      },
                      {
                        "id": 100000102,
                        "name": "菜单2",
                        "url": "system/menu/menu2/index",
                        "openStyle": 0,
                        "icon": "icon-unorderedlist"
                      },
                    ]
                  })
            }
			const routes = generateRoutes(menuList)
			const routesBtns = createRouteList(data)
			this.menuRoutes.push(...routes)
            this.allRoutes.push(...routesBtns)

			return this.menuRoutes
		},
		setSearchMenu(routers: RouteRecordRaw[]) {
			this.searchMenu = routers
		},
		setRoutes(routers: RouteRecordRaw[]) {
			this.routes = routers
		},
        updateNum(num) {
            this.menuRoutes.forEach(item => {
                if (item?.meta?.title == '库存管理') {
                    item.meta.title = `库存管理(${num})`;
                    item.children?.forEach(tem => {
                        if (tem?.meta?.title == '库存预警') {
                            tem.meta.title = `库存预警(${num})`;
                        }
                    })
                }
            })
        },
	}
})
